<%@page import="org.zty.db.AdminLTETools"%>
<%@page import="java.math.BigDecimal"%>
<%@page import="net.sf.json.JSONObject"%>
<%@page import="net.sf.json.JSONArray"%>
<%@page import="org.zty.db.JDBC"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="org.zty.domain.Pojo"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>

<%
	String cmd = request.getParameter("cmd");
	if("getTables".equals(cmd)){
		String url = request.getParameter("jdbc_url");
		String password = request.getParameter("jdbc_password");
		String username = request.getParameter("jdbc_username");
		String driver = request.getParameter("jdbc_driver");
		
		JDBC jdbc = new JDBC();
		List<String> l = jdbc.getAllTableName(driver, url, username, password);
		JSONArray jsonArray = new JSONArray();
		for(String s : l ){
			jsonArray.add(s);
		}
		out.println(jsonArray.toString());
	}else if("getTableDesc".equals(cmd)){
		String curTable = request.getParameter("curTable");
		String url = request.getParameter("jdbc_url");
		String password = request.getParameter("jdbc_password");
		String username = request.getParameter("jdbc_username");
		String driver = request.getParameter("jdbc_driver");
		JDBC jdbc = new JDBC();
		List<String[]> l = jdbc.getTableDesc(curTable, driver, url, username, password);
		StringBuffer str = new StringBuffer();
		for(String[] s : l ){
			StringBuffer sb = new StringBuffer();
			for(String stmp : s){
				sb.append(stmp);
				sb.append("     ");
			}
			sb.append("\r\n");
			str.append(sb);
		}
		JSONObject json = new JSONObject();
		//表结构信息
		json.put("str", str.toString());
		
		//生成代码
		StringBuffer pojoCodeStr = new StringBuffer();
		for(String[] s : l ){
			StringBuffer sb = new StringBuffer();
			sb.append("//列名");
			sb.append(s[0]);
			sb.append(" Type=");
			sb.append(s[1]);
			sb.append(" Length=");
			sb.append(s[2]);
			sb.append("DECIMAL_DIGITS=");
			sb.append(s[3]);
			sb.append(s[4].equals("1")?" Null":" Not Null");
			sb.append("\r\n");
			sb.append("private ");
			
			//整理类型
			String type = s[1].toLowerCase();
			if(type.contains("int")||type.contains("number")||type.contains("numeric")){
				sb.append("BigDecimal");
			}else if(type.contains("char")){
				sb.append("String");
			}else if(type.contains("time")||type.contains("date")){
				sb.append("Date");
			}else{
				sb.append("******unknowType******");
			}
			
			sb.append(" "+s[0].toLowerCase());
			sb.append(";\r\n");
			sb.append("\r\n");
			pojoCodeStr.append(sb);
		}
		json.put("pojoCodeStr", pojoCodeStr.toString());
		json.put("srcArray",JSONArray.fromObject(l));
		out.println(json.toString());
		System.out.println(json.toString());
	}else if("createCurTable".equals(cmd)){
		String curTable = request.getParameter("curTable");
		String url = request.getParameter("jdbc_url");
		String password = request.getParameter("jdbc_password");
		String username = request.getParameter("jdbc_username");
		String driver = request.getParameter("jdbc_driver");
		AdminLTETools adminLTETools = new AdminLTETools();
		adminLTETools.createCurTable(curTable, driver, url, username, password);
		adminLTETools.createJSfile();
		out.println(new JSONObject());
	}else if("createAll".equals(cmd)){
		String tables = request.getParameter("tables");
		String[] tableNameList = tables.split(",");
		String url = request.getParameter("jdbc_url");
		String password = request.getParameter("jdbc_password");
		String username = request.getParameter("jdbc_username");
		String driver = request.getParameter("jdbc_driver");
		AdminLTETools adminLTETools = new AdminLTETools();
		for(String curTable:tableNameList){
			adminLTETools.createCurTable(curTable, driver, url, username, password);
		}
		adminLTETools.createJSfile();
		out.println(new JSONObject());
	}
%>

